﻿
using System.Windows;
using MyFloridaHouse.libSvUtilities;
using System;
using MyFloridaHouse.libSvOpenDistricts.Dialogs;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {
        FileSaveTypeDlg fileSaveTypeDlg = null;

        private bool CmdFileExportCanExecute(object parameter)
        {
            return ((districtPlan.districtCount > 0)&(!isDialogOpen));
        }

        private void CmdFileSaveExecute(object parameter)
        {
            if (!isDialogOpen)
            {
                ReserveDialog(CmdFileSave);
                fileSaveTypeDlg = new FileSaveTypeDlg(OnMemberPropertyChanged, DisplayPanelForDialogs);
                fileSaveTypeDlg.Start(OnFileSaveGetFormatTypeDone);
            }
        }

        private void OnFileSaveGetFormatTypeDone(bool result)
        {
            try
            {
                if (!result)
                {
                    ReleaseDialog(CmdFileImportCloud);
                }
                else
                {
                    string filter;
                    string defaultExtension;
                    OutputFileGenerator outputFileGenerator;

                    if (districtPlan.SetupFileExport(fileSaveTypeDlg.districtPlanFormatType, out filter, out defaultExtension, out outputFileGenerator))
                    {
                        if (LocalFileManager.SaveFileLocal(outputFileGenerator, filter, defaultExtension))
                        {
                            MessageBox.Show("File Save Complete");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to save file : " + ex.Message);
            }
            finally
            {
                ReleaseDialog(CmdFileImportCloud);
            }
        }
    }
}
